package jpa4azure.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.persistence.EntityManagerFactory;
import javax.persistence.spi.PersistenceProvider;
import javax.persistence.spi.PersistenceUnitInfo;
import javax.persistence.spi.ProviderUtil;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

public class AzureProvider implements PersistenceProvider {

	final QName PROPERTY = new QName("", "property");
	final QName NAME = new QName("", "name");
	final QName VALUE = new QName("", "value");



	private Map<String, EntityManagerFactory> entityManagers;
	
	public AzureProvider() {
		entityManagers = new HashMap<String, EntityManagerFactory>();
	}
	
	public EntityManagerFactory createContainerEntityManagerFactory(
			PersistenceUnitInfo info, Map map) {
		throw new UnsupportedOperationException("");
	}

	
	public EntityManagerFactory createEntityManagerFactory(String key, Map map) {
		if (entityManagers.containsKey(key))
			return entityManagers.get(key);
		AzureEntityManagerFactory f;
		try {
			f = _createEntityManagerFactory(key, map);
			if (f != null)
				entityManagers.put(key, f);
			return f;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XMLStreamException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}

	private AzureEntityManagerFactory _createEntityManagerFactory(String key, Map map) throws IOException, XMLStreamException {
		XMLInputFactory inputFactory= XMLInputFactory.newInstance();
		InputStream input = findAssembly();
		XMLEventReader  xmlEventReader =inputFactory.createXMLEventReader(input);
		
		String storageAccountName=null;
		String storageAccountKey=null;
		for ( XMLEvent e; xmlEventReader.hasNext();) {
			 e = xmlEventReader.nextEvent();
			if (e.isStartElement()) {
				StartElement se = e.asStartElement();
				if (se.getName().equals(PROPERTY)) {
					Attribute name = se.getAttributeByName(NAME);
					Attribute value = se.getAttributeByName(VALUE);
					if ("azure.account.name".equals(name.getValue()))
						storageAccountName = value.getValue();
					if ("azure.account.key".equals(name.getValue()))
						storageAccountKey = value.getValue();
					
				}
			}
		}
		return new AzureEntityManagerFactory(storageAccountName, storageAccountKey, map);
	}

	public ProviderUtil getProviderUtil() {
		// TODO Auto-generated method stub
		return null;
	}
	
	public InputStream findAssembly() throws IOException {
		ClassLoader loader = Thread.currentThread().getContextClassLoader();
		return loader.getResourceAsStream("META-INF/persistence.xml");
		
	}

}
